using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.NoticeConfigs;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.EventBus;
using Microsoft.Extensions.DependencyInjection;
using MimeKit;
using Newtonsoft.Json;

namespace Admin.NET.Application.EventBus;

/// <summary>
/// 邮件发送消息订阅,类型为1
/// </summary>
public class EmailEventSubscribe : IEventSubscriber, ISingleton, IPrivateDependency
{
	private readonly IServiceProvider _serviceProvider;

	public EmailEventSubscribe(IServiceProvider serviceProvider)
	{
		_serviceProvider = serviceProvider;
	}

	/// <summary>
	/// 发送通知邮件
	/// </summary>
	/// <param name="context"></param>
	/// <returns></returns>
	[EventSubscribe("1:SendBillNotice")]
	[EventSubscribe("1:SendGateNotice")]
	[EventSubscribe("1:SendPaymentNotice")]
	[EventSubscribe("1:SendEarlyWarningNotice")]
	public async Task SendNotice(EventHandlerExecutingContext context)
	{
		using IServiceScope scope = _serviceProvider.CreateScope();
		SqlSugarRepository<EmailConfig> _repEmail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailConfig>>();
		SqlSugarRepository<EmailRecord> _repRecord = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailRecord>>();
		EventPayload payload = (EventPayload)context.Source.Payload;
		BillNoticePayload owners = JsonConvert.DeserializeObject<BillNoticePayload>(payload.Data);
		EmailConfig config = await _repEmail.GetFirstAsync((EmailConfig f) => f.TenantId == (long?)payload.TenantId);
		if (config == null)
		{
			return;
		}
		EMailUtil util = new EMailUtil();
		List<MailboxAddress> address = (from w in owners.Owners
			where !string.IsNullOrWhiteSpace(w.Email)
			select w into s
			select new MailboxAddress(s.Name, s.Email)).ToList();
		if (address.Count != 0)
		{
			if (config.Remark != null)
			{
				owners.Body = owners.Body + "\r\n" + config.Remark;
			}
			await util.SendEmailWithRecord(_repRecord, config, address, owners.Subject, owners.Body, payload.UserId);
		}
	}

	/// <summary>
	/// 发送邮件给管理者
	/// </summary>
	/// <param name="context"></param>
	/// <returns></returns>
	[EventSubscribe("10:SendNoticeToManager")]
	public async Task SendNoticeToManager(EventHandlerExecutingContext context)
	{
		using IServiceScope scope = _serviceProvider.CreateScope();
		SqlSugarRepository<EmailConfig> _repEmail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailConfig>>();
		SqlSugarRepository<EmailRecord> _repRecord = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailRecord>>();
		EventPayload payload = (EventPayload)context.Source.Payload;
		ManagerNoticePayload owners = JsonConvert.DeserializeObject<ManagerNoticePayload>(payload.Data);
		EmailConfig config = await _repEmail.GetFirstAsync((EmailConfig f) => f.TenantId == (long?)payload.TenantId);
		if (config == null)
		{
			return;
		}
		EMailUtil util = new EMailUtil();
		List<MailboxAddress> address = (from w in owners.Owners
			where !string.IsNullOrWhiteSpace(w.Email)
			select w into s
			select new MailboxAddress(s.RealName, s.Email)).ToList();
		if (address.Count != 0)
		{
			if (config.Remark != null)
			{
				owners.Body = owners.Body + "\r\n" + config.Remark;
			}
			await util.SendEmailWithRecord(_repRecord, config, address, owners.Subject, owners.Body, payload.UserId);
		}
	}
}
